package com.qinsong.myapplication.controller.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.qinsong.myapplication.R;
import com.qinsong.myapplication.model.bean.InvitationInfo;
import com.qinsong.myapplication.model.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 邀请信息列表适配器
 */
public class InvitationAdapter extends BaseAdapter {
    private Context mContext;
    private List<InvitationInfo> minvitationInfos = new ArrayList<>();
    private InvitationInfo invitationInfo;
    private OnClickListener mOnClickListener;
    public InvitationAdapter(Context context,OnClickListener onClickListener) {
        mContext = context;
        mOnClickListener = onClickListener;
    }

    /**
     * 刷新数据
     * @param invitationInfos
     */
    public void refresh(List<InvitationInfo> invitationInfos){
        if (invitationInfos != null && invitationInfos.size()>=0){
            //先清空数据
            minvitationInfos.clear();
            minvitationInfos.addAll(invitationInfos);
            notifyDataSetChanged();
        }

    }

    @Override
    public int getCount() {
        return minvitationInfos==null ? 0:minvitationInfos.size();

    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获取或者创建viewHolder
        ViewHolder holder = null;
        if (convertView==null){
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_invite,null);

            holder.name = convertView.findViewById(R.id.invita_name);
            holder.reason = convertView.findViewById(R.id.invita_reason);
            holder.accept = convertView.findViewById(R.id.bt_invita_accept);
            holder.reject = convertView.findViewById(R.id.bt_invita_reject);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        //获取当前item数据
        invitationInfo = minvitationInfos.get(position);
        //显示当前item数据
        UserInfo userInfo = invitationInfo.getUserInfo();
        if (userInfo!=null){
            //名称
            holder.name.setText(invitationInfo.getUserInfo().getName());

            //先隐藏接受按钮
            holder.accept.setVisibility(View.GONE);

            //隐藏拒绝按钮
            holder.reject.setVisibility(View.GONE);
            //原因
            //新的邀请信息
            if (invitationInfo.getStatus()==InvitationInfo.InvitationStatus.NEW_INVITE){
                if (invitationInfo.getReason()==null){
                    holder.reason.setText("就是想加你！");
                }else{
                    holder.reason.setText(invitationInfo.getReason());
                }

                holder.accept.setVisibility(View.VISIBLE);
                holder.reject.setVisibility(View.VISIBLE);
            }
            //接受了邀请
            else if (invitationInfo.getStatus()==InvitationInfo.InvitationStatus.INVITE_ACCEPT){
                if (invitationInfo.getReason()==null){
                    holder.reason.setText("接受邀请！");
                }else{
                    holder.reason.setText(invitationInfo.getReason());
                }
            }
            //邀请已经被接受
            else if (invitationInfo.getStatus()==InvitationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER){
                if (invitationInfo.getReason()==null){
                    holder.reason.setText("邀请被接受！");
                }else{
                    holder.reason.setText(invitationInfo.getReason());
                }
            }
            //按钮的处理
            //接受按钮处理
            holder.accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnClickListener.Accept(invitationInfo);
                }
            });
            //拒绝按钮的处理
            holder.reject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnClickListener.Reject(invitationInfo);
                }
            });
        }
        //返回view
        return convertView;
    }
    private class  ViewHolder{
            private TextView name;
            private TextView reason;
            private Button accept;
            private Button reject;
    }
    /**
     * 定义一个接口类处理点击事件
     */
    public interface OnClickListener{
        //接受按钮点击事件
        public void Accept(InvitationInfo invitationInfo);
        //拒绝按钮点击事件
        void Reject(InvitationInfo invitationInfo);
    }
}
